package com.graduation.cdm.funactivity.view;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.graduation.cdm.funactivity.MyApplication;
import com.graduation.cdm.funactivity.R;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import static com.graduation.cdm.funactivity.util.GlobalConstant.preUrl;
import static com.graduation.cdm.funactivity.util.TestPhoneEmail.isEmailId;
import static com.graduation.cdm.funactivity.util.TestPhoneEmail.isMobileId;

public class RegisteActivity extends AppCompatActivity {

    @InjectView(R.id.iv_bank)
    ImageView ivBank;
    @InjectView(R.id.ttop)
    RelativeLayout ttop;
    @InjectView(R.id.add_name)
    EditText addName;
    @InjectView(R.id.add_psw)
    EditText addPsw;
    @InjectView(R.id.add_psw_again)
    EditText addPswAgain;
    @InjectView(R.id.add_phone)
    EditText addPhone;
    @InjectView(R.id.add_email)
    EditText addEmail;
    @InjectView(R.id.editText2)
    EditText editText2;
    @InjectView(R.id.check)
    RadioGroup check;
    @InjectView(R.id.add_sure)
    Button addSure;
    @InjectView(R.id.add_back)
    Button addBack;
    @InjectView(R.id.check_woman)
    RadioButton checkWoman;
    @InjectView(R.id.check_man)
    RadioButton checkMan;

    String userName;
    String password;
    String userSex;
    String userPhone;
    String userEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registe);
        ButterKnife.inject(this);
    }

    @OnClick({R.id.iv_bank, R.id.add_sure, R.id.add_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_bank:
                finish();
                break;
            case R.id.add_sure:
                userName = addName.getText().toString();
                password = addPsw.getText().toString();
                userPhone = addPhone.getText().toString();
                userEmail = addEmail.getText().toString();
                if (checkMan.isChecked()){
                    userSex = "男";
                }else {
                    userSex = "女";
                }
                if (isMobileId(userPhone) || isEmailId(userEmail)){
                    registeRequest();
                }else {
                    Toast.makeText(getApplicationContext(),"手机号码或者邮箱格式错误",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.add_back:
                finish();
                break;
        }
    }

    private void registeRequest(){
        String url = preUrl+"user/registe";
        StringRequest registeRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                System.out.println("onResponse="+s.trim());
                Toast.makeText(getApplicationContext(),"注册成功，请登录！",Toast.LENGTH_SHORT).show();
                finish();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                System.out.println("onErrorResponse="+volleyError.toString());

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map=new HashMap<>();
                map.put("userName",userName);
                map.put("password",password);
                map.put("userSex",userSex);
                map.put("userPhone",userPhone);
                map.put("userEmail",userEmail);
                return map;
            }
        };
        MyApplication.getRequestQueue().add(registeRequest);
    }
}
